package activity;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;

import com.example.jaden.washeradministrator.*;
import com.example.jaden.washeradministrator.MyApplication;

import org.json.JSONException;
import org.json.JSONObject;

import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.List;

import customControls.WasherAdapter;
import dormitory.Dormitory;
import washer.Washer;

public class WasherPage extends AppCompatActivity implements CompoundButton.OnCheckedChangeListener {

    TextView dormloc;
    TextView washnumber;
    ListView washerlistview;
    ImageButton addwasher;
    WasherAdapter washerAdapter;
    MyApplication application;
    List<Washer> washerList;
    boolean isfree=false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.washerpage);
        //将当前活动加入list中进行管理
        ActivityManagement.activityList.add(this);
        getInstance();
        initwasher();
        addwasher.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialogShow();
            }
        });
    }
    protected void onDestroy() {
        ActivityManagement.KillOneActivity();
        super.onDestroy();
    }
/*
  //加载toolbar菜单文件
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.toolbar,menu);
        return true;
    }
    //处理菜单中按钮的点击事件
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()){
            case R.id.add:
                dialogShow();
                break;
        }
        return true;
    }
    */

    //获取控件实例
    private void getInstance() {
        dormloc=(TextView)findViewById(R.id.textView_dormloc_washerpage);
        washnumber=(TextView)findViewById(R.id.textView_washnumber_washerpage);
        washerlistview =(ListView)findViewById(R.id.ListView_washer_washerpage);
        addwasher=(ImageButton)findViewById(R.id.imageButton_add_washerpage);
        application= MyApplication.getInstance();
        washerList=application.getWasherList();
    }
    //显示洗衣机
    private void initwasher()
    {
        try {
            String url = "http://47.96.159.236:8080/Wash/Washer?method=getWasher";
            JSONObject userJson = new JSONObject();
            userJson.put("DormID",application.getCurDorm().getDormID());
            String messageJson = String.valueOf(userJson);
            TransferData.GetWasherList(url, messageJson);
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (JSONException e) {
            e.printStackTrace();
        }
        while (true) {
            if (application.isGetWasherOk()) {
                application.setGetWasherOk(false);
                List<Washer> washerList = (List<Washer>) application.getResponseMessage().getData();
                application.setWasherList(washerList);
                if(washerList==null||washerList.isEmpty())
                    washerAdapter=new WasherAdapter(WasherPage.this,R.layout.washer_listview_item_washingmachine,new ArrayList<Washer>());
                else
                    washerAdapter=new WasherAdapter(WasherPage.this,R.layout.washer_listview_item_washingmachine,washerList);
                washerlistview.setAdapter(washerAdapter);
                break;
            }
        }
        Dormitory dormitory=application.getCurDorm();
        dormloc.setText(dormitory.getDormLocation());
        washnumber.setText(String.valueOf(dormitory.getWasherNumber()));

    }

    //点击新增洗衣机的+号按钮
    private void dialogShow()
    {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        LayoutInflater inflater = LayoutInflater.from(this);
        View v = inflater.inflate(R.layout.addwasher_dialog, null);
        TextView dormID=(TextView)v.findViewById(R.id.textView_dormID_addwasherdialog);
        RadioButton free=(RadioButton)v.findViewById(R.id.radioButton_free_addwasherdialog);
        RadioButton busy=(RadioButton)v.findViewById(R.id.radioButton_busy_addwasherdialog);
        Button back=(Button)v.findViewById(R.id.button_back_addwasherdialog);
        Button confirm=(Button)v.findViewById(R.id.button_confirm_addwasherdialog);
        free.setOnCheckedChangeListener(this);
        busy.setOnCheckedChangeListener(this);
        dormID.setText("宿舍楼ID:"+String.valueOf(application.getCurDorm().getDormID()));

        final Dialog dialog=builder.create();
        dialog.show();
        dialog.getWindow().setContentView(v);
        dialog.getWindow().setGravity(Gravity.BOTTOM);

        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
        confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                    /**
                     * 增加洗衣机
                     */
                    try {
                        String url="http://47.96.159.236:8080/Wash/Washer?method=insertWasher";
                        JSONObject userJson=new JSONObject();
                        userJson.put("DormID",application.getCurDorm().getDormID());
                        userJson.put("IsFree", isfree);
                        String messageJson=String.valueOf(userJson);
                        TransferData.AddWasher(url,messageJson);
                    } catch (MalformedURLException e) {
                        e.printStackTrace();
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    while(true) {
                        if(application.isAddWasherOK()) {
                            application.setAddWasherOK(false);
                            //Order order= (Order) application.getResponseMessage().getData();
                            //application.
                            initwasher();
                            dialog.dismiss();
                            break;
                        }
                    }
                }

        });
    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        switch (buttonView.getId()) {
            case R.id.radioButton_free_addwasherdialog:
                isfree=true;
                break;
            case R.id.radioButton_busy_addwasherdialog:
                isfree=false;
                break;
        }
    }
}
